/**
 * Created by luxi on 2017/1/3.
 */
var Squelize = require('sequelize');
var squelize = require('../../models/index');
var Crypto = require('crypto')
var User = squelize.define('user',{
    id: {
        type: Squelize.INTEGER,
        primaryKey: true
    },
    username:{
        type: Squelize.STRING(50)
    },
    password: {
        type: Squelize.STRING(50)
    }
},{
    timestamps: false,
    freezeTableName: true
});

module.exports = {
    addUser: function (req, res, next) {
        var param = req.body;
        User.create({
            username: param.username,
            password: Crypto.createHash('md5').update(param.password).digest('hex')
        }).then(function(){
            res.json({
                "msg": "注册成功"
            })
        }).catch(function(err){
            console.log('failed: ' + err);
        })
    },
    login: function(req, res, next){
        var param = req.body;
        var password = Crypto.createHash('md5').update(param.password).digest('hex');
        User.find({
            where: {
                username: param.username,
                password: password
            }
        }).then(function(p){
            if(p){
                res.json({
                    "msg": "登录成功"
                })
            }else{
                res.json({
                    "msg": "登录失败"
                })
            }

        }).catch(function(err){
            console.log('failed: ' + err);
        })
    }
};





